package com.view;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.ScrollPaneConstants;

import com.controller.MainWindowController;
import com.view.utils.PlaneButton;
import com.view.utils.SkyPlaneButton;

public class MainWindow extends JFrame{

	private static MainWindow instancia = null;
	private ImagePanel taxiLanePanel;
	public static ImageIcon iconoAvion;
	public static ImageIcon iconoSky;
	public static ImageIcon iconoChafa;
	private JScrollPane scrollPaneEstacionamiento;
	private JPanel planePanel;
	private JPanel waitPanel;
	private ImagePanel holdingPatternPanel;
	private JScrollPane skyScroll;
	
	
	/**
	 * Instancia la Ventana.
	 * @return VentanaAplicacion
	 */
	public static final MainWindow getInstance(){
		if(instancia == null) {
			instancia = new MainWindow();
		}
		return instancia;
	}
	
	/**
	 * Create the application.
	 */
	public MainWindow() {
		super();
		iconoAvion = new ImageIcon(MainWindow.class.getResource("/images/background/plane.png"));
		iconoChafa = new ImageIcon(MainWindow.class.getResource("/images/background/dunno.png"));
		iconoSky = new ImageIcon(MainWindow.class.getResource("/images/background/Volaris.gif"));
		
		initialize();
		setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		setBounds(100, 100, 947, 788);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLocationRelativeTo(null);
		
		//frame.getContentPane().add(imagePanel, BorderLayout.CENTER);
		
		taxiLanePanel = new ImagePanel(ImagePanel.TAXI_LANE_BACKGROUND);
		
		scrollPaneEstacionamiento = new JScrollPane();
		scrollPaneEstacionamiento.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		scrollPaneEstacionamiento.setViewportView(taxiLanePanel);
		taxiLanePanel.setLayout(new GridLayout(0, 16, 0, 0));
		scrollPaneEstacionamiento.setSize(taxiLanePanel.getPreferredSize());
		
		scrollPaneEstacionamiento.getHorizontalScrollBar().setValue(taxiLanePanel.getImage().getWidth(null));
		scrollPaneEstacionamiento.getHorizontalScrollBar().setUnitIncrement(100);
		
		
		JButton despegar_bttn = new JButton("Despegar");
		despegar_bttn.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(!MainWindowController.departure()){
					System.out.println("Vacio, hija");
				}
			}
		});
		
		planePanel = new NewPlanePanel();
		
		waitPanel = new JPanel();
		
		
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(planePanel, GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(10)
							.addComponent(despegar_bttn, GroupLayout.PREFERRED_SIZE, 145, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(waitPanel, GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE))
						.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
							.addContainerGap()
							.addComponent(scrollPaneEstacionamiento, GroupLayout.DEFAULT_SIZE, 810, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(scrollPaneEstacionamiento, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(despegar_bttn, GroupLayout.PREFERRED_SIZE, 75, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(planePanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.UNRELATED)
					.addComponent(waitPanel, GroupLayout.PREFERRED_SIZE, 283, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(24, Short.MAX_VALUE))
		);

		skyScroll = new JScrollPane();
		skyScroll.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
		skyScroll.setSize(new Dimension(1444, 136));
		waitPanel.add(skyScroll);
		
		holdingPatternPanel = new ImagePanel(ImagePanel.SKY_BACKGROUND);
		waitPanel.add(holdingPatternPanel);
		getContentPane().setLayout(groupLayout);
	
	}
	
	public void wipeTaxiLane(){
		taxiLanePanel.removeAll();
		taxiLanePanel.repaint();
	}
	
	public void wipeHoldingPattern(){
		holdingPatternPanel.removeAll();
		holdingPatternPanel.repaint();
	}
	
	public void drawSky(String matricula, int pasajeros, int turbocina){
		JButton plane = new SkyPlaneButton(matricula, pasajeros, turbocina);
		plane.setIcon(iconoSky);
		holdingPatternPanel.add(plane);
		
		//SwingUtilities.updateComponentTreeUI(scrollPaneEstacionamiento);
		skyScroll.validate();
		validate();
	}
	
	public void drawTaxiLane(String matricula, int pasajeros, int turbocina){
		JButton plane = new PlaneButton(matricula, pasajeros, turbocina);
		taxiLanePanel.add(plane);
		
		//SwingUtilities.updateComponentTreeUI(scrollPaneEstacionamiento);
		scrollPaneEstacionamiento.validate();
		validate();
	}
	
}
